// store的配置文件

import {createStore} from 'redux'

// 初始化数据
const initState = {
  age: 18,
  hobby: [
    '唱','跳','rap','打篮球'
  ]
}

// 统一管理action的type
const actionType = {
  AGEADD: 'AGEADD',
  HOBBYADD: 'HOBBYADD'
}

// 生成action对象的函数
export const actionCreator = {
  ageAdd: ()=>({type: actionType.AGEADD}),
  hobbyAdd: (item)=>({type: actionType.HOBBYADD, item})
}

// 修改数据
function reducer (state = initState, action) {
  switch (action.type) {
    case actionType.AGEADD:
      return {
        // 返回一个完整的state对象
        ...state,
        age: state.age + 1
      }
    case actionType.HOBBYADD:
    console.log(action);
      let arr = state.hobby;
      arr.push(action.item);
      return {
        ...state,
        hobby: arr
      }
  
    default:
      return state
  }
}

// 创建仓库
const store = createStore(reducer);

export default store